//
//  SXSlideViewController.m
//  ShiXun
//
//  Created by lanou on 15/10/24.
//  Copyright © 2015年 张琪. All rights reserved.
//

#import "SXSlideViewController.h"
#import "UIView+UIView.h"
#import "AppDelegate.h"

#define kDrawerWidth (kScreenWidth / 4) // 打开左侧窗时，右视图显示的宽度
#define kRightPageScale  0.8  //打开左侧窗时，右视图缩放比例

#define kLeftPageScale 0.7 // 左侧视图初始比例
#define kLeftViewX (- 100) // 左视图初始偏移量

@interface SXSlideViewController ()

@property (nonatomic, strong) UIViewController *leftVC;
@property (nonatomic, strong) UIViewController *rightVC;
@property (nonatomic, strong) UITapGestureRecognizer *tap;

@end

@implementation SXSlideViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
}

- (instancetype)initWithLeftViewController:(UIViewController *)leftVC rightViewController:(UIViewController *)rightVC
{
    self = [super init];
    if (self) {
        _leftVC = leftVC;
        _leftVC.view.transform = CGAffineTransformMakeScale(kLeftPageScale, kLeftPageScale);
        _leftVC.view.x = kLeftViewX;
        [self.view addSubview:_leftVC.view];
        
        _rightVC = rightVC;
        [self.view addSubview:_rightVC.view];
        
        // 左边缘轻扫手势
        UIScreenEdgePanGestureRecognizer *edgePan = [[UIScreenEdgePanGestureRecognizer alloc] initWithTarget:self action:@selector(edgePan:)];
        edgePan.edges = UIRectEdgeLeft;
        [_rightVC.view addGestureRecognizer:edgePan];
        
        // 轻拍手势（抽屉出来后轻拍左页面抽屉回去）
        _tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(doTap)];
    }
    return self;
}

// 左边缘轻扫手势抽屉效果
- (void)edgePan:(UIScreenEdgePanGestureRecognizer *)edgePan
{
    CGPoint translaton = [edgePan translationInView:_rightVC.view];
    CGFloat newX = translaton.x;
    CGFloat rightScale = 1 - newX * (1 - kRightPageScale) / (kScreenWidth - kDrawerWidth) > kRightPageScale ? 1 - newX * (1 - kRightPageScale) / (kScreenWidth - kDrawerWidth) : kRightPageScale;
    CGFloat leftScale = (1 - kLeftPageScale) * newX / (kScreenWidth - kDrawerWidth) + 0.7;
    leftScale = leftScale < 1 ? leftScale : 1;
    
    _rightVC.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, rightScale, rightScale);
    _rightVC.view.x = newX < (kScreenWidth - kDrawerWidth) ? newX : (kScreenWidth - kDrawerWidth);
    
    _leftVC.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, leftScale, leftScale);
    _leftVC.view.x =  - kLeftViewX * newX / (kScreenWidth - kDrawerWidth) + kLeftViewX < 0 ? - kLeftViewX * newX / (kScreenWidth - kDrawerWidth) + kLeftViewX : 0;
    
    if (edgePan.state == UIGestureRecognizerStateEnded) {
        if (_rightVC.view.x > (kScreenWidth - kDrawerWidth) / 2) {
            [self openLeftView];
        }
        else
        {
            [self closeLeftView];
        }
    }
}

// 轻拍手势恢复抽屉
- (void)doTap
{
    [self closeLeftView];
}

// 关闭左视图
- (void)closeLeftView
{
    [UIView animateWithDuration:0.5 animations:^{
        _rightVC.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1, 1);
        _rightVC.view.x = 0;
        AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
        [delegate.rootRightVC.topView addSubview:delegate.rootRightVC.leftButton];
        
        _leftVC.view.transform = CGAffineTransformMakeScale(kLeftPageScale, kLeftPageScale);
        _leftVC.view.x = kLeftViewX;
    }];
    [_rightVC.view removeGestureRecognizer:_tap];
    for (UIView *view in _rightVC.view.subviews) {
        view.userInteractionEnabled = YES;
    }
}

// 打开左视图
- (void)openLeftView
{
    [UIView animateWithDuration:0.5 animations:^{
        _rightVC.view.transform = CGAffineTransformScale(CGAffineTransformIdentity, kRightPageScale, kRightPageScale);
        _rightVC.view.x = (kScreenWidth - kDrawerWidth);
        AppDelegate *delegate = [[UIApplication sharedApplication] delegate];
        [delegate.rootRightVC.leftButton removeFromSuperview];
        
        _leftVC.view.transform = CGAffineTransformMakeScale(1, 1);
        _leftVC.view.x = 0;

    }];
    [_rightVC.view addGestureRecognizer:_tap];
    for (UIView *view in _rightVC.view.subviews) {
        view.userInteractionEnabled = NO;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
